package com.joker.airoleplayplatformback.task;

import com.joker.airoleplayplatformback.service.PerformanceMonitorService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * 性能报告定时任务
 * @author feixiang.li
 * @since 2025-09-27
 */
@Slf4j
@Component
public class PerformanceReportTask {

    @Autowired
    private PerformanceMonitorService performanceMonitorService;

    /**
     * 每10分钟输出一次性能报告
     */
    @Scheduled(fixedRate = 600000) // 10分钟 = 600000毫秒
    public void generatePerformanceReport() {
        try {
            String report = performanceMonitorService.getPerformanceReport();
            log.info("定时性能报告: {}", report);
            
            // 检查性能瓶颈
            performanceMonitorService.checkPerformanceBottleneck();
            
        } catch (Exception e) {
            log.error("生成性能报告失败", e);
        }
    }

    /**
     * 每小时清理一次统计数据（可选，根据需要启用）
     */
    // @Scheduled(fixedRate = 3600000) // 1小时 = 3600000毫秒
    public void clearOldStatistics() {
        try {
            log.info("定时清理性能统计数据");
            performanceMonitorService.clearStatistics();
        } catch (Exception e) {
            log.error("清理性能统计数据失败", e);
        }
    }
}
